package com.plantpoint.controller;

import com.plantpoint.service.impl.UserServiceImpl;
import com.plantpoint.entity.bean.CommonResult;
import com.plantpoint.entity.dto.UserLoginDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/user")
public class UserController {

    private UserServiceImpl userService;

    public UserController(UserServiceImpl userService) {
        this.userService = userService;
    }

    //返回token
    @PostMapping("/login")
    public CommonResult<String> login(@RequestBody UserLoginDTO userLoginDTO) {
        System.out.println(userLoginDTO);
        String token = userService.login(userLoginDTO);

        if (token.equals("error:40029")){
            return new CommonResult<>(40029,"ERROR","code 无效");
        }else if (token.equals("error:40163")){
            return new CommonResult<>(40163,"ERROR","code 已使用过");
        }

        if (token.equals("")){
            return new CommonResult<>(50001,"用户信息不完整","");
        }
        System.out.println(token);
        return new CommonResult<>(20000,"OK",token);
    }
}
